/*
  ivl_trig.c  -  implementation of trigonometric functions and their inverses

  This file is part of the IVL Library (LIBIVL).
  
  LIBIVL - library for interval computations 
  
  Copyright (c) 2007 Mateusz Paprocki

  The IVL Library is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  The IVL Library is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with the IVL Library; see the file LICENCE.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#include "interval.h"

/*
  Compute a quadrant, which means z = floor (x * 2/pi). This function is
  exact and does not depend on the sign of its argument. Quadrant computation
  has to be so explicit because the inverse of pi requires several times 
  greater precision than the initial argument precision. Function will
  return the required precision. 
*/
static mp_prec_t ivl_quadrant(mpz_ptr z, mpfr_srcptr x) {
	mp_prec_t prec;
	mp_exp_t expo;
	ivl_t ivl;
	
	prec = mpfr_get_prec(x);
	
	if (mpfr_zero_p(x))
		mpz_set_ui(z, 0);
	else { 
		ivl_init2(ivl, prec);		
	
		while (1) {						
			ivl_const_pi(ivl);
			ivl_u_div(ivl, 2, ivl);
			
			ivl_mul_f(ivl, ivl, x);
			ivl_floor(ivl, ivl);
			
			if (!ivl_point_p(ivl)) {
				prec += BITS_PER_LIMB;
				ivl_set_prec(ivl, prec);
			} else
				break;
		}
			
		expo = mpfr_get_z_exp(z, IVL_LEFT(ivl));  
		mpz_mul_2exp(z, z, expo);
	
		ivl_clear(ivl);
	}
	
	return prec;
}

int ivl_sin(ivl_ptr ivl, ivl_srcptr x) {
	int inexact = 0, inexact_left, inexact_right;
	unsigned int z_mod4_left, z_mod4_right;
	mpz_t z, z_left, z_right;
	mpfr_t tmp;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		if (mpfr_inf_p(IVL_LEFT(x)) && mpfr_inf_p(IVL_RIGHT(x))) {
			if (mpfr_sgn(IVL_LEFT(x)) == mpfr_sgn(IVL_RIGHT(x)))
				ivl->domain = IVL_UNDEFINED;
			else {
				inexact = ivl_make_s(ivl, -1, 1);
				ivl->domain = x->domain;
			}
		} else {
			mpz_init(z_left); mpz_init(z_right);

			ivl_quadrant(z_left, IVL_LEFT(x));
			ivl_quadrant(z_right, IVL_RIGHT(x));

			if ((mpz_sgn(z_left) < 0) && (mpz_sgn(z_right) > 0)) {
				if (mpz_cmp_si(z_left, -1) < 0)
					inexact_left = mpfr_set_si(IVL_LEFT(ivl), -1, GMP_RNDD);
				else
					inexact_left = mpfr_sin(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
				
				if (mpz_cmp_ui(z_left, 0) > 0)
					inexact_right = mpfr_set_ui(IVL_RIGHT(ivl), 1, GMP_RNDU);
				else
					inexact_right = mpfr_sin(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
			} else {
				mpz_init(z);
				
				mpz_sub(z, z_right, z_left);
				
				if (mpz_cmp_ui(z, 4) >= 0)
					ivl_make_s(ivl, -1, 1);
				else {
					mpz_mod_ui(z, z_left, 4);
					z_mod4_left = mpz_get_ui(z);
				
					mpz_mod_ui(z, z_right, 4);
					z_mod4_right = mpz_get_ui(z);

					switch (z_mod4_left) {
						case 0:
							switch (z_mod4_right) {
								case 0:
									inexact_left = mpfr_sin(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
									inexact_right = mpfr_sin(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
									break;
								case 1:
									mpfr_init2(tmp, ivl_get_prec(ivl));
								
									inexact_left = mpfr_sin(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
									inexact_left |= mpfr_sin(tmp, IVL_RIGHT(x), GMP_RNDD);
								
									mpfr_min(IVL_LEFT(ivl), tmp, IVL_LEFT(ivl), GMP_RNDD);
								
									inexact_right = mpfr_set_ui(IVL_RIGHT(ivl), 1, GMP_RNDU);
									
									mpfr_clear(tmp);
									break;
								case 2:
									inexact_left = mpfr_sin(IVL_LEFT(ivl), IVL_RIGHT(x), GMP_RNDD);
									inexact_right = mpfr_set_ui(IVL_RIGHT(ivl), 1, GMP_RNDU);
									break;
								case 3:
									ivl_make_s(ivl, -1, 1);
									break;
							}
						
							break;
						case 1:
							switch (z_mod4_right) {
								case 0:
									inexact_left = mpfr_set_si(IVL_LEFT(ivl), -1, GMP_RNDD);
								
									mpfr_init2(tmp, ivl_get_prec(ivl));
								
									inexact_right = mpfr_sin(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
									inexact_right |= mpfr_sin(tmp, IVL_RIGHT(x), GMP_RNDU);
								
									mpfr_max(IVL_RIGHT(ivl), tmp, IVL_RIGHT(ivl), GMP_RNDU);

									mpfr_clear(tmp);
									break;
								case 1:
									inexact_left = mpfr_sin(IVL_LEFT(ivl), IVL_RIGHT(x), GMP_RNDD);
									inexact_right = mpfr_sin(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
									break;
								case 2:
									inexact_left = mpfr_sin(IVL_LEFT(ivl), IVL_RIGHT(x), GMP_RNDD);
									inexact_right = mpfr_sin(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
									break;
								case 3:
									inexact_left = mpfr_set_si(IVL_LEFT(ivl), -1, GMP_RNDD);
									inexact_right = mpfr_sin(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
									break;
							}
						
							break;
						case 2:
							switch (z_mod4_right) {
								case 0:
									inexact_left = mpfr_set_si(IVL_LEFT(ivl), -1, GMP_RNDD);
									inexact_right = mpfr_sin(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
									break;
								case 1:
									ivl_make_s(ivl, -1, 1);
									break;
								case 2:
									inexact_left = mpfr_sin(IVL_LEFT(ivl), IVL_RIGHT(x), GMP_RNDD);
									inexact_right = mpfr_sin(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
									break;
								case 3:
									inexact_left = mpfr_set_si(IVL_LEFT(ivl), -1, GMP_RNDD);
								
									mpfr_init2(tmp, ivl_get_prec(ivl));
								
									inexact_right = mpfr_sin(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
									inexact_right |= mpfr_sin(tmp, IVL_RIGHT(x), GMP_RNDU);
								
									mpfr_max(IVL_RIGHT(ivl), tmp, IVL_RIGHT(ivl), GMP_RNDU);

									mpfr_clear(tmp);
									break;
							}
						
							break;
						case 3:
							switch (z_mod4_right) {
								case 0:
									inexact_left = mpfr_sin(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
									inexact_right = mpfr_sin(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
									break;
								case 1:
									inexact_left = mpfr_sin(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
									inexact_right = mpfr_set_ui(IVL_RIGHT(ivl), 1, GMP_RNDU);
									break;
								case 2:
									mpfr_init2(tmp, ivl_get_prec(ivl));
								
									inexact_left = mpfr_sin(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
									inexact_left |= mpfr_sin(tmp, IVL_RIGHT(x), GMP_RNDD);
								
									mpfr_min(IVL_LEFT(ivl), tmp, IVL_LEFT(ivl), GMP_RNDD);
								
									inexact_right = mpfr_set_ui(IVL_RIGHT(ivl), 1, GMP_RNDU);
									
									mpfr_clear(tmp);
									break;
								case 3:
									inexact_left = mpfr_sin(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
									inexact_right = mpfr_sin(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
									break;
							}
						
							break;
					}
				}
				
				mpz_clear(z);
			}

			IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
			
			mpz_clear(z_right);
			mpz_clear(z_left);
		}
	}
	
	return inexact;
}

int ivl_cos(ivl_ptr ivl, ivl_srcptr x) {
	int inexact = 0, inexact_left, inexact_right;
	unsigned int z_mod4_left, z_mod4_right;
	mpz_t z, z_left, z_right;
	mpfr_t tmp;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		if (mpfr_inf_p(IVL_LEFT(x)) && mpfr_inf_p(IVL_RIGHT(x))) {
			if (mpfr_sgn(IVL_LEFT(x)) == mpfr_sgn(IVL_RIGHT(x)))
				ivl->domain = IVL_UNDEFINED;
			else {
				inexact = ivl_make_s(ivl, -1, 1);
				ivl->domain = x->domain;
			}
		} else {
			mpz_init(z_left); mpz_init(z_right);

			ivl_quadrant(z_left, IVL_LEFT(x));
			ivl_quadrant(z_right, IVL_RIGHT(x));
			
			mpfr_init2(tmp, ivl_get_prec(ivl));

			if ((mpz_sgn(z_left) < 0) && (mpz_sgn(z_right) > 0)) {
				if ((mpz_cmp_si(z_left, -2) < 0) || (mpz_cmp_ui(z_left, 1) > 0))
					inexact_left = mpfr_set_si(IVL_LEFT(ivl), -1, GMP_RNDD);
				else {
					inexact_left = mpfr_cos(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
					inexact_left |= mpfr_cos(tmp, IVL_RIGHT(x), GMP_RNDD);
					
					mpfr_min(IVL_LEFT(ivl), tmp, IVL_LEFT(ivl), GMP_RNDD);
				}

				inexact_right = mpfr_set_ui(IVL_RIGHT(ivl), 1, GMP_RNDU);
			} else {
				mpz_init(z);
				
				mpz_sub(z, z_right, z_left);
				
				if (mpz_cmp_ui(z, 4) >= 0)
					ivl_make_s(ivl, -1, 1);
				else {
					mpz_mod_ui(z, z_left, 4);
					z_mod4_left = mpz_get_ui(z);
				
					mpz_mod_ui(z, z_right, 4);
					z_mod4_right = mpz_get_ui(z);

					switch (z_mod4_left) {
						case 0:
							switch (z_mod4_right) {
								case 0:
									inexact_left = mpfr_cos(IVL_LEFT(ivl), IVL_RIGHT(x), GMP_RNDD);
									inexact_right = mpfr_cos(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
									break;
								case 1:
									inexact_left = mpfr_cos(IVL_LEFT(ivl), IVL_RIGHT(x), GMP_RNDD);
									inexact_right = mpfr_cos(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
									break;
								case 2:
									inexact_left = mpfr_set_si(IVL_LEFT(ivl), -1, GMP_RNDD);
									inexact_right = mpfr_cos(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
									break;
								case 3:
									inexact_left = mpfr_set_si(IVL_LEFT(ivl), -1, GMP_RNDD);
								
									mpfr_init2(tmp, ivl_get_prec(ivl));
								
									inexact_right = mpfr_cos(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
									inexact_right |= mpfr_cos(tmp, IVL_RIGHT(x), GMP_RNDU);
								
									mpfr_max(IVL_RIGHT(ivl), tmp, IVL_RIGHT(ivl), GMP_RNDU);

									mpfr_clear(tmp);
									break;
							}
						
							break;
						case 1:
							switch (z_mod4_right) {
								case 0:
									ivl_make_s(ivl, -1, 1);
									break;
								case 1:
									inexact_left = mpfr_cos(IVL_LEFT(ivl), IVL_RIGHT(x), GMP_RNDD);
									inexact_right = mpfr_cos(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
									break;
								case 2:
									inexact_left = mpfr_set_si(IVL_LEFT(ivl), -1, GMP_RNDD);
								
									mpfr_init2(tmp, ivl_get_prec(ivl));
								
									inexact_right = mpfr_cos(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
									inexact_right |= mpfr_cos(tmp, IVL_RIGHT(x), GMP_RNDU);
								
									mpfr_max(IVL_RIGHT(ivl), tmp, IVL_RIGHT(ivl), GMP_RNDU);

									mpfr_clear(tmp);
									break;
								case 3:
									inexact_left = mpfr_set_si(IVL_LEFT(ivl), -1, GMP_RNDD);
									inexact_right = mpfr_cos(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
									break;
							}
						
							break;
						case 2:
							switch (z_mod4_right) {
								case 0:
									inexact_left = mpfr_cos(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
									inexact_right = mpfr_set_ui(IVL_RIGHT(ivl), 1, GMP_RNDU);
									break;
								case 1:
									mpfr_init2(tmp, ivl_get_prec(ivl));
								
									inexact_left = mpfr_cos(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
									inexact_left |= mpfr_cos(tmp, IVL_RIGHT(x), GMP_RNDD);
								
									mpfr_min(IVL_LEFT(ivl), tmp, IVL_LEFT(ivl), GMP_RNDD);
								
									inexact_right = mpfr_set_ui(IVL_RIGHT(ivl), 1, GMP_RNDU);
									
									mpfr_clear(tmp);
									break;
								case 2:
									inexact_left = mpfr_cos(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
									inexact_right = mpfr_cos(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
									break;
								case 3:
									inexact_left = mpfr_cos(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
									inexact_right = mpfr_cos(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
									break;
							}
						
							break;
						case 3:
							switch (z_mod4_right) {
								case 0:
									mpfr_init2(tmp, ivl_get_prec(ivl));
								
									inexact_left = mpfr_cos(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
									inexact_left |= mpfr_cos(tmp, IVL_RIGHT(x), GMP_RNDD);
								
									mpfr_min(IVL_LEFT(ivl), tmp, IVL_LEFT(ivl), GMP_RNDD);
								
									inexact_right = mpfr_set_ui(IVL_RIGHT(ivl), 1, GMP_RNDU);
									
									mpfr_clear(tmp);
									break;
								case 1:
									inexact_left = mpfr_cos(IVL_LEFT(ivl), IVL_RIGHT(x), GMP_RNDD);
									inexact_right = mpfr_set_ui(IVL_RIGHT(ivl), 1, GMP_RNDU);
									break;
								case 2:
									ivl_make_s(ivl, -1, 1);
									break;
								case 3:
									inexact_left = mpfr_cos(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
									inexact_right = mpfr_cos(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
									break;
							}

							break;
					}
				}
				
				mpz_clear(z);
			}

			IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
			
			mpfr_clear(tmp);
			
			mpz_clear(z_right);
			mpz_clear(z_left);
		}
	}
	
	return inexact;
}

int ivl_tan(ivl_ptr ivl, ivl_srcptr x) {
	int inexact = 0, inexact_left, inexact_right;
	mpz_t z, z_left, z_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		if (mpfr_inf_p(IVL_LEFT(x)) && mpfr_inf_p(IVL_RIGHT(x))) {
			if (mpfr_sgn(IVL_LEFT(x)) == mpfr_sgn(IVL_RIGHT(x)))
				ivl->domain = IVL_UNDEFINED;
			else
				ivl_make_unbounded(ivl, IVL_PARTIAL);
		} else {
			mpz_init(z_left);
			mpz_init(z_right);

			ivl_quadrant(z_left, IVL_LEFT(x));
			ivl_quadrant(z_right, IVL_RIGHT(x));

			ivl->domain = x->domain;

			if ((mpz_sgn(z_left) < 0) && (mpz_sgn(z_right) > 0)) {
				if (mpz_cmp_si(z_left, -1) < 0)
					ivl_make_unbounded(ivl, IVL_PARTIAL);
				else {
					if (mpz_cmp_ui(z_left, 0) > 0)
						ivl_make_unbounded(ivl, IVL_PARTIAL);
					else {
						inexact_left = mpfr_tan(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
						inexact_right = mpfr_tan(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
					}
				}
			} else {
				mpz_init(z);
				
				mpz_sub(z, z_right, z_left);
				
				if ((mpz_cmp_ui(z, 2) >= 0) || (mpz_even_p(z_left) && mpz_odd_p(z_right)))
					ivl_make_unbounded(ivl, IVL_PARTIAL);
				else {
					inexact_left = mpfr_tan(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
					inexact_right = mpfr_tan(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
				}
				
				mpz_clear(z);
			}
			
			IVL_CHECK_NO_DOM(ivl, inexact, inexact_left, inexact_right);

			mpz_clear(z_right);
			mpz_clear(z_left);
		}
	}
	
	return inexact;
}

int ivl_csc(ivl_ptr ivl, ivl_srcptr x) {
	int inexact = 0, inexact_left, inexact_right;
	mpz_t z, z_left, z_right;
	mpfr_t tmp;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		if (mpfr_inf_p(IVL_LEFT(x)) && mpfr_inf_p(IVL_RIGHT(x))) {
			if (mpfr_sgn(IVL_LEFT(x)) == mpfr_sgn(IVL_RIGHT(x)))
				ivl->domain = IVL_UNDEFINED;
			else
				ivl_make_unbounded(ivl, IVL_PARTIAL);
		} else {
			mpz_init(z_left);
			mpz_init(z_right);

			ivl_quadrant(z_left, IVL_LEFT(x));
			ivl_quadrant(z_right, IVL_RIGHT(x));

			if ((mpz_sgn(z_left) < 0) && (mpz_sgn(z_right) > 0))
				ivl_make_unbounded(ivl, IVL_PARTIAL);
			else {
				mpz_init(z);
				
				mpz_sub(z, z_right, z_left);
				
				if (mpz_cmp_ui(z, 2) >= 0)
					ivl_make_unbounded(ivl, IVL_PARTIAL);
				else {
					ivl->domain = x->domain;
					
					if (mpz_odd_p(z_left)) {
						if (mpz_even_p(z_right))
							ivl_make_unbounded(ivl, IVL_PARTIAL);
						else {
							mpz_mod_ui(z, z_left, 4);
							
							if (mpz_get_ui(z) == 1) {
								inexact_left = mpfr_csc(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
								inexact_right = mpfr_csc(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
							} else {
								inexact_left = mpfr_csc(IVL_LEFT(ivl), IVL_RIGHT(x), GMP_RNDD);
								inexact_right = mpfr_csc(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
							}
						}
					} else {
						mpz_mod_ui(z, z_left, 4);
						
						if (mpz_even_p(z_right)) {
							if (mpz_get_ui(z) == 0) {
								inexact_left = mpfr_csc(IVL_LEFT(ivl), IVL_RIGHT(x), GMP_RNDD);
								inexact_right = mpfr_csc(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
							} else {
								inexact_left = mpfr_csc(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
								inexact_right = mpfr_csc(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
							}
						} else {
							mpfr_init2(tmp, ivl_get_prec(ivl));
							
							if (mpz_get_ui(z) == 0) {
								inexact_left = mpfr_set_ui(IVL_LEFT(ivl), 1, GMP_RNDD);
								
								inexact_right = mpfr_csc(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
								inexact_right |= mpfr_csc(tmp, IVL_RIGHT(x), GMP_RNDU);
								
								mpfr_max(IVL_RIGHT(ivl), tmp, IVL_RIGHT(ivl), GMP_RNDU);
							} else {
								inexact_left = mpfr_csc(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
								inexact_left |= mpfr_csc(tmp, IVL_RIGHT(x), GMP_RNDD);
								
								mpfr_min(IVL_LEFT(ivl), tmp, IVL_LEFT(ivl), GMP_RNDD);
								
								inexact_right = mpfr_set_si(IVL_RIGHT(ivl), -1, GMP_RNDU);
							}
							
							mpfr_clear(tmp);
						}
					}
				}
				
				IVL_CHECK_NO_DOM(ivl, inexact, inexact_left, inexact_right);
				
				mpz_clear(z);
			}

			mpz_clear(z_right);
			mpz_clear(z_left);
		}
	}
	
	return inexact;
}

int ivl_sec(ivl_ptr ivl, ivl_srcptr x) {
	int inexact = 0, inexact_left, inexact_right;
	mpz_t z, z_left, z_right;
	mpfr_t tmp;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		if (mpfr_inf_p(IVL_LEFT(x)) && mpfr_inf_p(IVL_RIGHT(x))) {
			if (mpfr_sgn(IVL_LEFT(x)) == mpfr_sgn(IVL_RIGHT(x)))
				ivl->domain = IVL_UNDEFINED;
			else
				ivl_make_unbounded(ivl, IVL_PARTIAL);
		} else {
			mpz_init(z);
			mpz_init(z_left);
			mpz_init(z_right);

			ivl_quadrant(z_left, IVL_LEFT(x));
			ivl_quadrant(z_right, IVL_RIGHT(x));

			ivl->domain = x->domain;
			
			mpfr_init2(tmp, ivl_get_prec(ivl));

			if ((mpz_sgn(z_left) < 0) && (mpz_sgn(z_right) > 0)) {
				if (mpz_cmp_si(z_left, -1) < 0)
					ivl_make_unbounded(ivl, IVL_PARTIAL);
				else {
					if (mpz_cmp_ui(z_left, 0) > 0)
						ivl_make_unbounded(ivl, IVL_PARTIAL);
					else {
						inexact_left = mpfr_set_ui(IVL_LEFT(ivl), 1, GMP_RNDD);
								
						inexact_right = mpfr_sec(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
						inexact_right |= mpfr_sec(tmp, IVL_RIGHT(x), GMP_RNDU);
								
						mpfr_max(IVL_RIGHT(ivl), tmp, IVL_RIGHT(ivl), GMP_RNDU);
					}
				}
			} else {
				mpz_init(z);
				
				mpz_sub(z, z_right, z_left);
				
				if (mpz_cmp_ui(z, 2) >= 0)
					ivl_make_unbounded(ivl, IVL_PARTIAL);
				else {
					ivl->domain = x->domain;
					
					if (mpz_even_p(z_left)) {
						if (mpz_odd_p(z_right))
							ivl_make_unbounded(ivl, IVL_PARTIAL);
						else {
							mpz_mod_ui(z, z_left, 4);
							
							if (mpz_get_ui(z) == 0) {
								inexact_left = mpfr_sec(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
								inexact_right = mpfr_sec(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
							} else {
								inexact_left = mpfr_sec(IVL_LEFT(ivl), IVL_RIGHT(x), GMP_RNDD);
								inexact_right = mpfr_sec(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
							}
						}
					} else {
						mpz_mod_ui(z, z_left, 4);
						
						if (mpz_odd_p(z_right)) {
							if (mpz_get_ui(z) == 1) {
								inexact_left = mpfr_sec(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
								inexact_right = mpfr_sec(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
							} else {
								inexact_left = mpfr_sec(IVL_LEFT(ivl), IVL_RIGHT(x), GMP_RNDD);
								inexact_right = mpfr_sec(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
							}
						} else {
							if (mpz_get_ui(z) == 1) {
								inexact_left = mpfr_sec(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
								inexact_left |= mpfr_sec(tmp, IVL_RIGHT(x), GMP_RNDD);
								
								mpfr_min(IVL_LEFT(ivl), tmp, IVL_LEFT(ivl), GMP_RNDD);
								
								inexact_right = mpfr_set_si(IVL_RIGHT(ivl), -1, GMP_RNDU);
							} else {
								inexact_left = mpfr_set_ui(IVL_LEFT(ivl), 1, GMP_RNDD);
								
								inexact_right = mpfr_sec(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
								inexact_right |= mpfr_sec(tmp, IVL_RIGHT(x), GMP_RNDU);
								
								mpfr_max(IVL_RIGHT(ivl), tmp, IVL_RIGHT(ivl), GMP_RNDU);
							}
						}
					}
				}

				mpz_clear(z);  
			}
			
			IVL_CHECK_NO_DOM(ivl, inexact, inexact_left, inexact_right);

			mpfr_clear(tmp);  
			
			mpz_clear(z_right);
			mpz_clear(z_left);
		}
	}
	
	return inexact;
}

int ivl_cot(ivl_ptr ivl, ivl_srcptr x) {
	int inexact = 0, inexact_left, inexact_right;
	mpz_t z, z_left, z_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		if (mpfr_inf_p(IVL_LEFT(x)) && mpfr_inf_p(IVL_RIGHT(x))) {
			if (mpfr_sgn(IVL_LEFT(x)) == mpfr_sgn(IVL_RIGHT(x)))
				ivl->domain = IVL_UNDEFINED;
			else
				ivl_make_unbounded(ivl, IVL_PARTIAL);
		} else {
			mpz_init(z_left);
			mpz_init(z_right);

			ivl_quadrant(z_left, IVL_LEFT(x));
			ivl_quadrant(z_right, IVL_RIGHT(x));

			if ((mpz_sgn(z_left) < 0) && (mpz_sgn(z_right) > 0))
				ivl_make_unbounded(ivl, IVL_PARTIAL);
			else {
				mpz_init(z);
				
				mpz_sub(z, z_right, z_left);
				
				if ((mpz_cmp_ui(z, 2) >= 0) || (mpz_odd_p(z_left) && mpz_even_p(z_right)))
					ivl_make_unbounded(ivl, IVL_PARTIAL);
				else {
					inexact_left = mpfr_tan(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
					inexact_right = mpfr_tan(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);
				}
				
				IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
				
				mpz_clear(z);
			}

			mpz_clear(z_right);
			mpz_clear(z_left);
		}
	}
	
	return inexact;
}

int ivl_asin(ivl_ptr ivl, ivl_srcptr x) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {				
		if (mpfr_cmp_si(IVL_LEFT(x), -1) < 0) {
			if (mpfr_cmp_si(IVL_RIGHT(x), -1) >= 0) {
				inexact_left = mpfr_const_pi(IVL_LEFT(ivl), GMP_RNDD);				
				inexact_left |= mpfr_div_si(IVL_LEFT(ivl), IVL_LEFT(ivl), -2, GMP_RNDD);
				
				ivl->domain = IVL_PARTIAL;
				
				if (mpfr_cmp_ui(IVL_RIGHT(x), 1) > 0) {
					inexact_right = mpfr_const_pi(IVL_RIGHT(ivl), GMP_RNDU);				
					inexact_right |= mpfr_div_ui(IVL_RIGHT(ivl), IVL_RIGHT(ivl), 2, GMP_RNDU);
				} else
					inexact_right = mpfr_asin(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);								
			} else
				ivl->domain = IVL_UNDEFINED;			
		} else {
			inexact_left = mpfr_asin(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
			
			if (mpfr_cmp_si(IVL_RIGHT(x), 1) <= 0) {
				inexact_right = mpfr_asin(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);								
				ivl->domain = x->domain;
			} else {
				inexact_right = mpfr_const_pi(IVL_RIGHT(ivl), GMP_RNDU);				
				inexact_right |= mpfr_div_ui(IVL_RIGHT(ivl), IVL_RIGHT(ivl), 2, GMP_RNDU);
				
				ivl->domain = IVL_PARTIAL;				
			}
		}

		IVL_CHECK_NO_DOM(ivl, inexact, inexact_left, inexact_right);		
	}
	
	return inexact;
}

int ivl_acos(ivl_ptr ivl, ivl_srcptr x) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {				
		if (mpfr_cmp_si(IVL_LEFT(x), -1) < 0) {
			if (mpfr_cmp_si(IVL_RIGHT(x), -1) >= 0) {
				inexact_right = mpfr_const_pi(IVL_RIGHT(ivl), GMP_RNDU);								
				
				ivl->domain = IVL_PARTIAL;
				
				if (mpfr_cmp_ui(IVL_RIGHT(x), 1) > 0)
					inexact_left = mpfr_set_ui(IVL_LEFT(ivl), 0, GMP_RNDD);				
				else
					inexact_left = mpfr_acos(IVL_LEFT(ivl), IVL_RIGHT(x), GMP_RNDD);								
			} else
				ivl->domain = IVL_UNDEFINED;			
		} else {
			inexact_right = mpfr_acos(IVL_RIGHT(ivl), IVL_LEFT(x), GMP_RNDU);
			
			if (mpfr_cmp_si(IVL_RIGHT(x), 1) <= 0) {
				inexact_left = mpfr_acos(IVL_LEFT(ivl), IVL_RIGHT(x), GMP_RNDD);								
				ivl->domain = x->domain;
			} else {
				inexact_left = mpfr_set_ui(IVL_LEFT(ivl), 0, GMP_RNDD);				
				ivl->domain = IVL_PARTIAL;				
			}
		}

		IVL_CHECK_NO_DOM(ivl, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_atan(ivl_ptr ivl, ivl_srcptr x) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {	
		inexact_left = mpfr_atan(IVL_LEFT(ivl), IVL_LEFT(x), GMP_RNDD);
		inexact_right = mpfr_atan(IVL_RIGHT(ivl), IVL_RIGHT(x), GMP_RNDU);

		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

